package com.blueocean_health.care.management.service;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.pinyin.Chinese2PinyinUtils;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.domain.base.NurseCheckWork;
import com.blueocean_health.care.management.domain.base.NurseGroupRelation;
import com.blueocean_health.care.management.domain.base.NurseWorkerInfo;
import com.blueocean_health.care.management.domain.base.OrderInfo;
import com.blueocean_health.care.management.em.OrderFlagEnum;
import com.blueocean_health.care.management.em.OrderStatusEnum;
import com.blueocean_health.care.management.em.WorkStatusEnum;
import com.blueocean_health.care.management.mapper.NurseCheckWorkMapper;
import com.blueocean_health.care.management.mapper.NurseGroupRelationMapper;
import com.blueocean_health.care.management.mapper.NurseWorkerInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 考勤管理，每日凌晨添加当日考勤原始数据
 * @Author ChenPeng cp
 */
@Service
public class CheckWorkScheduleService {

	@Autowired
	private NurseCheckWorkMapper nurseCheckWorkMapper;

    @Autowired
    private NurseGroupRelationMapper nurseGroupRelationMapper;

    @Autowired
    private NurseWorkerInfoMapper workerMainInfoMapper;
    @Value("${sys.schedule}")
	private boolean schedule;

    //使用cron表达式
	// 每天0点10分执行
	//@Scheduled(cron="0 0 16 * * ?")
	@Scheduled(cron="0 10 0 * * ?")
//    @Scheduled(cron=" 0 0/1 * * * *")
	public void schedule(){
		if(!schedule){
			return;
		}
		//注意, 如果方法中的程序在周期内(30m)内无法执行完成, 会开启两次, 自行控制
		
		System.err.println("当前日期：" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));

        String todayStr = TimeUtils.getTime(TimeType.yyyy_MM_dd);
        Date today = TimeUtils.getDateByString(todayStr,TimeType.yyyy_MM_dd);

        Calendar requestEndZeroCalendar = Calendar.getInstance();
        requestEndZeroCalendar.setTime(today);
        requestEndZeroCalendar.add(Calendar.DAY_OF_YEAR, -1);
        Date yesterday = requestEndZeroCalendar.getTime();

        String yesterdayStr = TimeUtils.getStringByDate(yesterday, TimeType.yyyy_MM_dd);

        Date now = new Date();

        List<NurseCheckWork> nurseCheckWorks = nurseCheckWorkMapper.queryAllByWorkTime(yesterdayStr);
        List<NurseGroupRelation> allMaxInfo = nurseGroupRelationMapper.getAllMaxInfo();
        //如果没有昨日数据，新增
        if (nurseCheckWorks.size() < 1){
            allMaxInfo.forEach(nurseGroupRelation -> {
                NurseCheckWork work = new NurseCheckWork();
                work.setWorkTime(yesterday);
                work.setWorkId(nurseGroupRelation.getNurseId());

                NurseWorkerInfo workerMainInfo = workerMainInfoMapper.selectMainByWorkerId(nurseGroupRelation.getNurseId());

                if (workerMainInfo != null && !StringUtils.isEmpty(workerMainInfo.getWorkerName())){
                    work.setWorkName(workerMainInfo.getWorkerName());
                    work.setWorkPyname(Chinese2PinyinUtils.toPinyinAll(workerMainInfo.getWorkerName()));
                    work.setWorkStatus(WorkStatusEnum._0.getKey());
                    work.setGroupId(nurseGroupRelation.getGroupId());
                    work.setDeflag("0");
                    work.setUpdateTime(now);
                    work.setCreateTime(now);
                    nurseCheckWorkMapper.insert(work);

                    System.out.println("日期："+yesterdayStr+ " 小组："+work.getGroupId() +" 护工："+work.getWorkId() +"考勤记录补录成功");
                }
            });

            nurseCheckWorks = nurseCheckWorkMapper.queryAllByWorkTime(yesterdayStr);
        }


        List<NurseCheckWork> todayNurseCheckWorks = nurseCheckWorkMapper.queryAllByWorkTime(todayStr);
        //如果今日数据已经生成过，则不执行
        if (todayNurseCheckWorks.size() > 0){
            return;
        }

        //只录小组中护工的，不延续
        allMaxInfo.forEach(nurseGroupRelation -> {
            NurseCheckWork work = new NurseCheckWork();
            work.setWorkTime(today);
            work.setWorkId(nurseGroupRelation.getNurseId());
            
            NurseWorkerInfo workerMainInfo = workerMainInfoMapper.selectMainByWorkerId(nurseGroupRelation.getNurseId());

            if (workerMainInfo != null && !StringUtils.isEmpty(workerMainInfo.getWorkerName())){
                work.setWorkName(workerMainInfo.getWorkerName());
                work.setWorkPyname(Chinese2PinyinUtils.toPinyinAll(workerMainInfo.getWorkerName()));
                work.setWorkStatus(WorkStatusEnum._0.getKey());
                work.setGroupId(nurseGroupRelation.getGroupId());
                work.setDeflag("0");
                work.setUpdateTime(now);
                work.setCreateTime(now);
                nurseCheckWorkMapper.insert(work);

                System.out.println("日期："+todayStr+ " 小组："+work.getGroupId() +" 护工："+work.getWorkId() +"考勤记录添加成功");
            }
        });

        //延续昨天的人员
        /*nurseCheckWorks.forEach(nurseCheckWork -> {
            nurseCheckWork.setId(null);
           nurseCheckWork.setWorkTime(today);
           nurseCheckWork.setCreateTime(now);
           nurseCheckWork.setUpdateTime(now);
           nurseCheckWork.setWorkStatus(WorkStatusEnum._0.getKey());
           nurseCheckWorkMapper.insert(nurseCheckWork);
            System.out.println("日期："+todayStr+ " 小组："+nurseCheckWork.getGroupId() +" 护工："+nurseCheckWork.getWorkId() +"考勤记录添加成功");
        });*/
    }

}
